/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Cliente.Controller;

import Cliente.Entidade.Cliente;
import Cliente.View.ConsultarCliente;
import CommandConsultar.ConsultarCommand;
import Inteface.Log;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.ButtonGroup;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Caio
 */
public class ConsultarClientePresenter {

    public ConsultarCliente view = new ConsultarCliente();
    private Log log;

    public Log getLog() {
        return log;
    }

    public void setClientes(List<Cliente> clientes) {
        this.clientes = clientes;
    }

    public List<Cliente> getClientes() {
        return clientes;
    }
    private List<Cliente> clientes;
    private DefaultTableModel tableModel;
    private ListSelectionModel listModel;
    private ButtonGroup grupo = new ButtonGroup();

    public ConsultarClientePresenter(Log log) {
        grupo.add(view.getRadFeminino());
        grupo.add(view.getRadMasculino());
        this.log = log;
        view.setVisible(true);
        defineModelo();

        view.getBtnPesquisarNome().addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                consultaNome();
            }
        });

        view.getBtnFechar().addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                view.dispose();
            }
        });

        view.getBtnPesquisaSexo().addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                consultaSexo();
            }
        });


    }

    private void consultaSexo(){
        ConsultarCommand consulta = new ConsultarCommand(this);
        consulta.atualizaTabelaSexo();
    }
    
    private void consultaNome(){
         ConsultarCommand consulta = new ConsultarCommand(this);
          consulta.atualizaTabelaNome();
    }
    
    private void defineModelo() {

        setTableModel((DefaultTableModel) view.getTblCliente().getModel());
        listModel = view.getTblCliente().getSelectionModel();
        listModel.addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                if (e.getValueIsAdjusting()) {
                    mostraDetalheCliente();
                }
            }
        });
        view.getTblCliente().getColumnModel().getColumn(0).setPreferredWidth(50);
    }

    private void mostraDetalheCliente() {
        if (view.getTblCliente().getSelectedRow() != -1) {
            int indice = view.getTblCliente().getSelectedRow();
            view.getTxtIAC().setText(clientes.get(indice).getIAC());
            view.getTxtIMC().setText(clientes.get(indice).getIMC());
            view.getTxtRCQ().setText(clientes.get(indice).getRCQ());
        } else {
            view.getTxtIAC().setText("");
            view.getTxtIMC().setText("");
            view.getTxtRCQ().setText("");
        }
    }

    /**
     * @return the tableModel
     */
    public DefaultTableModel getTableModel() {
        return tableModel;
    }

    /**
     * @param tableModel the tableModel to set
     */
    public void setTableModel(DefaultTableModel tableModel) {
        this.tableModel = tableModel;
    }
}
